home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-08-27 | 11.3 KB | 522 lines | [TEXT/MPS ] |
- /*
- File: AppLib.cp
-
- Contains: The “guts” of a Macintosh application.
-
- Written by: Dave Falkenburg and many other SmartFriends™
-
- Copyright: © 1993-94 by Dave Falkenburg, all rights reserved.
-
- Change History (most recent first):
-
- */
-
- #ifdef SystemSevenOrLater
- #undef SystemSevenOrLater
- #endif
- #define SystemSevenOrLater 1
-
- #include <limits.h> // For LONG_MAX
-
- #include <Types.h>
- #include <Quickdraw.h>
- #include <Fonts.h>
- #include <Menus.h>
- #include <Windows.h>
- #include <Dialogs.h>
- #include <Desk.h>
- #include <Events.h>
- #include <AppleEvents.h>
- #include <DiskInit.h>
-
- #if qUseETO15Interfaces
- #include <Gestalt.h>
- #include <CodeFragments.h>
- #include <Devices.h>
- #else
- #include <GestaltEqu.h>
- #include <FragLoad.h>
- #endif
-
- #include <ToolUtils.h>
- #include <Traps.h>
- #include <LowMem.h>
-
- #include <Threads.h>
- #include <Drag.h>
- #include <Editions.h>
- #include <OCEStandardMail.h>
-
- #if qInlineInputAware
- #include <TextServices.h>
- #include <TSMTE.h>
- #endif
-
- #include "AppLib.h"
- #include "StandardMenus.h"
- #include "Window.h"
- #include "SplashWindow.h"
- #include "MailableWindow.h"
- #include "AppleEventHandling.h"
-
- #if qUseQuickDrawGX
- #include <graphics macintosh.h>
- #include <graphics routines.h>
- #include <PrintingManager.h>
- #endif
-
-
- // Function Prototypes
-
- void main(void);
- void MainEventLoop(void);
-
- void HandleMouseDown(TWindow * topWindowObj, EventRecord * anEvent);
- void HandleUpdate(EventRecord * anEvent);
- void HandleClose(WindowPtr aWindow);
-
-
- // Globals
-
- Boolean gDone = false;
- Boolean gMenuBarNeedsUpdate = true;
-
- Boolean gHasColorQuickdraw = false;
- Boolean gHasThreadManager = false;
- Boolean gHasDragManager = false;
- Boolean gHasAppleScript = false;
- Boolean gHasAOCE = false;
- Boolean gHasDisplayManager = false;
-
- GrafPtr gWindowManagerPort;
- Rect gDeskRectangle;
- RgnHandle gMouseRegion = nil;
-
- short gPreferencesRsrcRefNum;
-
- #if qInlineInputAware
- Boolean gHasTextServices = false;
- Boolean gHasTSMTE = false;
- #endif
-
- #if qUseQuickDrawGX
- Boolean gHasQuickDrawGX = false;
- long gQuickDrawGXVersion = 0;
- long gQuickDrawGXPrintingVersion = 0;
- gxGraphicsClient gQuickDrawGXClient;
-
-
- // PrintingEventOverride is our generic event handler for QuickDrawGX.
- // It alows us to handle events while the QuickDrawGX movable modal
- // printing dialogs are displayed.
-
- // ¿ Really should move to a GX-specific place ?
-
- OSErr
- PrintingEventOverrideForGX(EventRecord *anEvent, Boolean filterEvent)
- {
- if (!filterEvent)
- switch (anEvent->what)
- {
- case mouseDown:
- case keyDown:
- case autoKey:
- break;
-
- default:
- HandleEvent(anEvent);
- }
-
- return noErr;
- }
-
- #endif
-
-
- // Values that can be adjusted by other application code to change
- // the behavior of the MainEventLoop.
- //
- // Rules of thumb:
- //
- // Increase gXXXRunQuantum (and decrease gXXXSleepQuantum) when:
- // The application has many threads running that need time
- //
- // Decrease gXXXRunQuantum when:
- // Sending AppleEvents to other applications
- // Launching other applications
- // Running in the background
-
- unsigned long gForegroundRunQuantum = 0;
- unsigned long gForegroundSleepQuantum = GetCaretTime();
- unsigned long gBackgroundRunQuantum = 0;
- unsigned long gBackgroundSleepQuantum = LONG_MAX;
-
-
- // Globals used to “tune” the performance of MainEventLoop
- // (assume we’ll be starting in the foreground)
-
- static unsigned long gRunQuantum = gForegroundRunQuantum;
- static unsigned long gSleepQuantum = gForegroundSleepQuantum;
-
- #ifdef powerc
- #ifndef __MWERKS__
- QDGlobals qd;
- #endif
- #endif
-
- void
- main(void)
- {
- long feature;
-
- MaxApplZone();
- MoreMasters();
- MoreMasters();
- MoreMasters();
- MoreMasters();
-
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(nil);
-
-
- if (GetToolTrapAddress(_Unimplemented) == GetOSTrapAddress(_Gestalt))
- FatalErrorAlert(kCoreErrorStrings,kUnsupportedSystemSoftware);
-
- if (Gestalt(gestaltQuickdrawFeatures,&feature) == noErr)
- gHasColorQuickdraw = ((feature & (1 << gestaltHasColor)) != 0);
-
-
- TSplashWindow * splashWindow = new TSplashWindow;
-
-
- if ((Gestalt(gestaltAppleEventsAttr,&feature) == noErr) &&
- (feature & (1 << gestaltAppleEventsPresent)))
- {
- // Figure out if we need to do AppleEvent recording
- gHasAppleScript = (feature & (1 << gestaltScriptingSupport));
- }
- else
- FatalErrorAlert(kCoreErrorStrings,kUnsupportedSystemSoftware);
-
- #if qInlineInputAware
- if ((Gestalt(gestaltTSMgrVersion,&feature) == noErr) && (feature >= 1))
- {
- gHasTextServices = true;
- if (Gestalt(gestaltTSMTEAttr, &feature) == noErr)
- gHasTSMTE = (feature & (1 << gestaltTSMTEPresent));
- }
- #endif
-
- if (Gestalt(gestaltThreadMgrAttr,&feature) == noErr)
- {
- #ifdef powerc
- // If running on a PowerPC, make sure that we not only have the
- // 68K Thread Manager, but also the PowerPC shared library, too.
- // Because of the wonders of weak linking and out of memory errors
- // we need to also check to make sure that an entrypoint in the library
- // is there, too.
- if ((Ptr) NewThread != kUnresolvedSymbolAddress)
- gHasThreadManager = ((feature & ((1 << gestaltThreadMgrPresent) | (1 << gestaltThreadsLibraryPresent))) != 0);
- #else
- gHasThreadManager = ((feature & (1 << gestaltThreadMgrPresent)) != 0);
- #endif
- }
-
- // Check for and install Drag Manager callbacks
- if (Gestalt(gestaltDragMgrAttr,&feature) == noErr)
- {
- #ifdef powerc
- // If running on a PowerPC, make sure that we not only have the
- // 68K Drag Manager, but also the PowerPC shared library, too.
- if ((Ptr) NewDrag != kUnresolvedSymbolAddress)
- gHasDragManager = ((feature & ((1 << gestaltDragMgrPresent) | (1 << gestaltPPCDragLibPresent))) != 0);
- #else
- gHasDragManager = ((feature & (1 << gestaltDragMgrPresent)) != 0);
- #endif
-
- if (gHasDragManager)
- {
- InstallTrackingHandler(NewDragTrackingHandlerProc(CallWindowDragTrackingHandler),(WindowPtr) nil,nil);
- InstallReceiveHandler(NewDragReceiveHandlerProc(CallWindowDragReceiveHandler),(WindowPtr) nil,nil);
- }
- }
-
- // Check for Display Manager
- if (Gestalt(gestaltDisplayMgrAttr,&feature) == noErr)
- gHasDisplayManager = ((feature & (1 << gestaltDisplayMgrPresent)) != 0);
-
- // Check for and initialize AOCE Standard Mail package if it exists
- if ((Gestalt(gestaltSMPMailerVersion,&feature) == noErr) && (feature != 0))
- {
- #ifdef powerc
- if ((Ptr) SMPInitMailer != kUnresolvedSymbolAddress)
- gHasAOCE = (SMPInitMailer(kSMPVersion) == noErr);
- #else
- gHasAOCE = (SMPInitMailer(kSMPVersion) == noErr);
- #endif
- }
-
- #if qUseQuickDrawGX
- // Check for and initialize QuickDrawGX
- if (Gestalt(gestaltGXVersion, &gQuickDrawGXVersion) == noErr)
- if (Gestalt(gestaltGXPrintingMgrVersion,&gQuickDrawGXPrintingVersion) == noErr)
- #ifdef powerc
- if ((Ptr) GXEnterGraphics != kUnresolvedSymbolAddress)
- gHasQuickDrawGX = true;
- #else
- gHasQuickDrawGX = true;
- #endif
-
- if (gHasQuickDrawGX)
- {
- // gQuickDrawGXClient = GXNewGraphicsClient(nil, kGraphicsHeapSize, (gxClientAttribute) 0);
- GXEnterGraphics();
- GXInitPrinting();
- }
- #endif
-
- // Install our AppleEvent Handlers
- InstallAppleEventHandlers();
-
- // Setup desktop rectangle for dragging windows around
- GetWMgrPort(&gWindowManagerPort);
- gDeskRectangle = (**GetGrayRgn()).rgnBBox;
-
- // Get the default menubar
- SetMenuBar(GetNewMBar(rMenuBar));
- AddResMenu(GetMHandle(mApple),'DRVR');
-
- gPreferencesRsrcRefNum = OpenPreferencesResFile();
-
- if (SetupApplication() == noErr)
- {
- delete splashWindow; // get rid of the splash screen
- MainEventLoop();
- TearDownApplication();
- }
-
- #if qUseQuickDrawGX
- if (gHasQuickDrawGX) // Tear down QuickDrawGX
- {
- GXExitPrinting();
- // GXDisposeGraphicsClient(gQuickDrawGXClient); // DIES A HORRIBLE DEATH FOR ME?
- GXExitGraphics();
- }
- #endif
- }
-
-
- void
- MainEventLoop(void)
- {
- EventRecord anEvent;
- unsigned long nextTimeToCheckForEvents = 0;
-
- while (!gDone)
- {
- if (gMenuBarNeedsUpdate)
- {
- gMenuBarNeedsUpdate = false;
- DrawMenuBar();
- }
-
- if ((gRunQuantum == 0) ||
- (TickCount() > nextTimeToCheckForEvents))
- {
- nextTimeToCheckForEvents = TickCount() + gRunQuantum;
-
- (void) WaitNextEvent(everyEvent,&anEvent,gSleepQuantum,gMouseRegion);
-
- HandleEvent(&anEvent);
- }
-
- if (gHasThreadManager)
- YieldToAnyThread();
- }
- }
-
-
- void
- HandleEvent(EventRecord *anEvent)
- {
- TWindow * wobj;
-
- if (anEvent->what != updateEvt)
- wobj = GetWindowObject(FrontNonFloatingWindow());
- else
- wobj = GetWindowObject((WindowPtr) anEvent->message);
-
- if (wobj != nil)
- wobj->AdjustCursor(anEvent);
-
- if ((wobj != nil) && wobj->EventFilter(anEvent))
- return;
-
- else switch (anEvent->what)
- {
- case nullEvent:
- if (wobj != nil)
- wobj->Idle(anEvent);
- break;
-
- case mouseDown:
- HandleMouseDown(wobj,anEvent);
- break;
-
- case keyDown:
- case autoKey:
- if (anEvent->modifiers & cmdKey)
- HandleMenu(wobj,MenuKey((short) anEvent->message & charCodeMask));
- else if (wobj != nil)
- wobj->KeyDown(anEvent);
- break;
-
- case updateEvt:
- HandleUpdate(anEvent);
- break;
-
- case diskEvt:
- if (anEvent->message >> 16)
- {
- static Point where = {50,50};
- (void) DIBadMount(where,anEvent->message);
- }
- break;
-
- case osEvt:
- switch ((anEvent->message & osEvtMessageMask) >> 24)
- {
- case mouseMovedMessage:
- break;
-
- case suspendResumeMessage:
- if (anEvent->message & resumeFlag)
- {
- gRunQuantum = gForegroundRunQuantum;
- gSleepQuantum = gForegroundSleepQuantum;
- }
- else
- {
- gRunQuantum = gBackgroundRunQuantum;
- gSleepQuantum = gBackgroundSleepQuantum;
- }
-
- SuspendResumeWindows((anEvent->message & resumeFlag) != 0);
- if (anEvent->message & convertClipboardFlag)
- ConvertClipboard();
- break;
- }
- break;
-
- case kHighLevelEvent:
- (void) AEProcessAppleEvent(anEvent);
- break;
-
- default:
- break;
- }
- }
-
- void
- HandleMouseDown(TWindow * topWindowObj,EventRecord *anEvent)
- {
- WindowPtr aWindow;
- short partCode;
- TWindow *wobj;
-
- partCode = FindWindow(anEvent->where,&aWindow);
- wobj = GetWindowObject(aWindow);
- switch(partCode)
- {
- case inMenuBar:
- HandleMenu(topWindowObj,MenuSelect(anEvent->where));
- break;
-
- case inSysWindow:
- SystemClick(anEvent,aWindow);
- break;
-
- case inContent:
- if (wobj)
- {
- GrafPtr oldPort;
-
- GetPort(&oldPort);
- SetPort(aWindow);
- GlobalToLocal(&anEvent->where);
- wobj->Click(anEvent);
- SetPort(aWindow);
- }
- break;
-
- case inDrag:
- if (wobj)
- wobj->Drag(anEvent->where);
- break;
-
- case inGrow:
- if (wobj)
- wobj->Grow(anEvent->where);
- break;
-
- case inGoAway:
- if (TrackGoAway(aWindow,anEvent->where))
- HandleClose(aWindow);
- break;
-
- case inZoomIn:
- case inZoomOut:
- if (TrackBox(aWindow,anEvent->where,partCode) && (wobj))
- wobj->Zoom(partCode);
- break;
-
- default:
- break;
- }
- }
-
-
- void HandleUpdate(EventRecord * anEvent)
- {
- GrafPtr oldPort;
- WindowPtr aWindow = (WindowPtr) anEvent->message;
- TWindow * wobj;
-
- GetPort(&oldPort);
- SetPort(aWindow);
- BeginUpdate(aWindow);
-
- if ((wobj = GetWindowObject(aWindow)) != nil)
- wobj->Draw();
-
- EndUpdate(aWindow);
- SetPort(oldPort);
- }
-
- void
- HandleClose(WindowPtr aWindow)
- {
- short windowKind;
- TWindow *wobj;
-
- if (aWindow)
- {
- windowKind = ((WindowPeek) aWindow)->windowKind;
- if (windowKind < 0)
- {
- CloseDeskAcc(((WindowPeek)aWindow)->windowKind);
- }
- else if ( ((wobj = GetWindowObject(aWindow)) != nil) &&
- wobj->CanClose() &&
- wobj->Close() &&
- wobj->DeleteAfterClose())
- {
- delete wobj;
- }
- }
- }
-